package com.imooc.controller;

import com.imooc.base.BaseInfoProperties;
import com.imooc.config.MinIOConfig;
import com.imooc.entity.bo.UpdateUserBo;
import com.imooc.entity.pojo.Users;
import com.imooc.entity.vo.UsersVo;
import com.imooc.enums.FileTypeEnum;
import com.imooc.exceptions.GraceException;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.service.UsersService;
import com.imooc.utils.MinIOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/userInfo")
@Slf4j
public class UserInfoController extends BaseInfoProperties {
    @Autowired
    private UsersService usersService;
    @Autowired
    private MinIOConfig minIOConfig;
    @GetMapping("/query")
    public GraceJSONResult getUserInfo(@RequestParam("userId")String userId){
        Users user = usersService.findUserByUserId(userId);
        UsersVo userVo = new UsersVo();
        BeanUtils.copyProperties(user,userVo);
        // 4. set用户数据信息
        Integer myFollowsCounts =  0;
        Integer myFansCounts = 0;
        Integer vlogBeLikedCounts = 0;
        Integer vloggerBeLikedCounts = 0;
        if (redis.get(REDIS_MY_FOLLOWS_COUNTS + ":" + user.getId())!=null){
            myFollowsCounts = Integer.valueOf(redis.get(REDIS_MY_FOLLOWS_COUNTS + ":" + user.getId()));
        }
        if (redis.get(REDIS_MY_FANS_COUNTS + ":" + user.getId())!=null){
            myFansCounts = Integer.valueOf(redis.get(REDIS_MY_FANS_COUNTS + ":" + user.getId()));
        }
//        if (redis.get(REDIS_VLOG_BE_LIKED_COUNTS + ":" + user.getId())!=null){
//            vlogBeLikedCounts = Integer.valueOf(redis.get(REDIS_VLOG_BE_LIKED_COUNTS + ":" + user.getId()));
//        }
        if (redis.get(REDIS_VLOGGER_BE_LIKED_COUNTS + ":" + user.getId())!=null){
            vloggerBeLikedCounts = Integer.valueOf(redis.get(REDIS_VLOGGER_BE_LIKED_COUNTS + ":" + user.getId()));
        }
        userVo.setMyFollowsCounts(myFollowsCounts);
        userVo.setMyFansCounts(myFansCounts);
        userVo.setMyLikedVlogCounts(vlogBeLikedCounts);
        userVo.setTotalLikeMeCounts(vloggerBeLikedCounts+vlogBeLikedCounts);
        return GraceJSONResult.ok(userVo);
    }
    @PostMapping("/modifyUserInfo")
    public GraceJSONResult modifyUserInfo(@RequestBody UpdateUserBo updateUserBo,
                                          @RequestParam("type")Integer type){
        log.info("updateUserBo = " + updateUserBo);
        return GraceJSONResult.ok(usersService.updateUserInfo(updateUserBo,type));
    }
    @PostMapping("/modifyImage")
    public GraceJSONResult modifyImage(@RequestParam("userId")String userId,
                                       @RequestParam("type")Integer type,
                                       MultipartFile file){
        log.info("userId = " + userId);
        log.info("type = " + type);
        // 不是背景图也不是头像 则直接上传失败
        if(type!= FileTypeEnum.FACE.type&&type!=FileTypeEnum.BGIMG.type){
            GraceException.display(ResponseStatusEnum.FILE_FORMATTER_FAILD);
        }
        String filename = file.getOriginalFilename();
        try {
            MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String imgUrl = minIOConfig.getFileHost() + "/" + minIOConfig.getBucketName() + "/" + filename;
        Users user = usersService.findUserByUserId(userId);
        UpdateUserBo updateUserBo = new UpdateUserBo();
        if(type == FileTypeEnum.BGIMG.type){
            user.setBgImg(imgUrl);
            BeanUtils.copyProperties(user,updateUserBo);
            usersService.updateUserInfo(updateUserBo);
        }
        if(type == FileTypeEnum.FACE.type){
            user.setFace(imgUrl);
            BeanUtils.copyProperties(user,updateUserBo);
            usersService.updateUserInfo(updateUserBo);
        }
        return GraceJSONResult.ok(usersService.findUserByUserId(userId));
    }
}
